﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibKetoan;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmSocai.
	/// </summary>
	public class frmSocai : System.Windows.Forms.Form
	{
		//Khai bao gia tri mau cho form
		private int i_Red=255,i_Green=255,i_Blue=195;
		//End.
		//Khai bao bien su dung trong form.
		private AccessData a;
		private Print Print=new Print();
		private System.Data.DataRow r;
		private System.Data.DataTable dtDmtk;
		private System.Data.DataTable dtDmlk;
		private System.Data.DataSet dsData;
		private string sql="",s_Report="";
		private System.Windows.Forms.NumericUpDown numTuthang;
		private System.Windows.Forms.TextBox txtLoaikhoan;
		private System.Windows.Forms.TextBox txtSotaikhoan;
		private System.Windows.Forms.NumericUpDown numNam;
		private System.Windows.Forms.Button butIn;
		private LibList.List listTaikhoan;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label8;
		private LibList.List listLoaikhoan;
		private System.Windows.Forms.Button butKetthuc;
		private System.Windows.Forms.TextBox txtTenloaikhoan;
		private System.Windows.Forms.Label label13;
		private System.Windows.Forms.TextBox txtTentaikhoan;
		private System.Windows.Forms.NumericUpDown numDenthang;
		private System.Windows.Forms.Label lblTentk;
		private System.Windows.Forms.Label lblTenlk;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button butXem;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmSocai(AccessData acc)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=acc;			
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
			try
			{
				dtDmtk.Dispose();
				dtDmlk.Dispose();
				dsData.Dispose();
			}
			catch{};
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmSocai));
			this.numTuthang = new System.Windows.Forms.NumericUpDown();
			this.txtLoaikhoan = new System.Windows.Forms.TextBox();
			this.txtSotaikhoan = new System.Windows.Forms.TextBox();
			this.numNam = new System.Windows.Forms.NumericUpDown();
			this.butIn = new System.Windows.Forms.Button();
			this.label4 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.butKetthuc = new System.Windows.Forms.Button();
			this.txtTenloaikhoan = new System.Windows.Forms.TextBox();
			this.label13 = new System.Windows.Forms.Label();
			this.txtTentaikhoan = new System.Windows.Forms.TextBox();
			this.numDenthang = new System.Windows.Forms.NumericUpDown();
			this.lblTentk = new System.Windows.Forms.Label();
			this.lblTenlk = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.butXem = new System.Windows.Forms.Button();
			this.listLoaikhoan = new LibList.List();
			this.listTaikhoan = new LibList.List();
			((System.ComponentModel.ISupportInitialize)(this.numTuthang)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.numNam)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.numDenthang)).BeginInit();
			this.SuspendLayout();
			// 
			// numTuthang
			// 
			this.numTuthang.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.numTuthang.ForeColor = System.Drawing.Color.DarkBlue;
			this.numTuthang.Location = new System.Drawing.Point(79, 16);
			this.numTuthang.Maximum = new System.Decimal(new int[] {
																	   12,
																	   0,
																	   0,
																	   0});
			this.numTuthang.Minimum = new System.Decimal(new int[] {
																	   1,
																	   0,
																	   0,
																	   0});
			this.numTuthang.Name = "numTuthang";
			this.numTuthang.Size = new System.Drawing.Size(37, 21);
			this.numTuthang.TabIndex = 3;
			this.numTuthang.Value = new System.Decimal(new int[] {
																	 1,
																	 0,
																	 0,
																	 0});
			this.numTuthang.Enter += new System.EventHandler(this.numTuthang_Enter);
			this.numTuthang.KeyDown += new System.Windows.Forms.KeyEventHandler(this.event_KeyDown);
			this.numTuthang.Leave += new System.EventHandler(this.numTuthang_Leave);
			// 
			// txtLoaikhoan
			// 
			this.txtLoaikhoan.BackColor = System.Drawing.SystemColors.HighlightText;
			this.txtLoaikhoan.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			this.txtLoaikhoan.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtLoaikhoan.ForeColor = System.Drawing.Color.DarkBlue;
			this.txtLoaikhoan.Location = new System.Drawing.Point(79, 70);
			this.txtLoaikhoan.Name = "txtLoaikhoan";
			this.txtLoaikhoan.Size = new System.Drawing.Size(101, 21);
			this.txtLoaikhoan.TabIndex = 15;
			this.txtLoaikhoan.Text = "";
			this.txtLoaikhoan.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtLoaikhoan_KeyDown);
			this.txtLoaikhoan.Validated += new System.EventHandler(this.txtLoaikhoan_Validated);
			this.txtLoaikhoan.TextChanged += new System.EventHandler(this.txtLoaikhoan_TextChanged);
			this.txtLoaikhoan.DoubleClick += new System.EventHandler(this.txtLoaikhoan_DoubleClick);
			this.txtLoaikhoan.Leave += new System.EventHandler(this.numLoaikhoan_Leave);
			this.txtLoaikhoan.Enter += new System.EventHandler(this.txtLoaikhoan_Enter);
			// 
			// txtSotaikhoan
			// 
			this.txtSotaikhoan.BackColor = System.Drawing.SystemColors.HighlightText;
			this.txtSotaikhoan.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			this.txtSotaikhoan.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtSotaikhoan.ForeColor = System.Drawing.Color.DarkBlue;
			this.txtSotaikhoan.Location = new System.Drawing.Point(79, 43);
			this.txtSotaikhoan.Name = "txtSotaikhoan";
			this.txtSotaikhoan.Size = new System.Drawing.Size(101, 21);
			this.txtSotaikhoan.TabIndex = 11;
			this.txtSotaikhoan.Text = "";
			this.txtSotaikhoan.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtSotaikhoan_KeyDown);
			this.txtSotaikhoan.Validated += new System.EventHandler(this.txtSotaikhoan_Validated);
			this.txtSotaikhoan.TextChanged += new System.EventHandler(this.txtSotaikhoan_TextChanged);
			this.txtSotaikhoan.DoubleClick += new System.EventHandler(this.txtSotaikhoan_DoubleClick);
			this.txtSotaikhoan.Leave += new System.EventHandler(this.numSotaikhoan_Leave);
			this.txtSotaikhoan.Enter += new System.EventHandler(this.txtSotaikhoan_Enter);
			// 
			// numNam
			// 
			this.numNam.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.numNam.ForeColor = System.Drawing.Color.DarkBlue;
			this.numNam.Location = new System.Drawing.Point(216, 16);
			this.numNam.Maximum = new System.Decimal(new int[] {
																   3004,
																   0,
																   0,
																   0});
			this.numNam.Minimum = new System.Decimal(new int[] {
																   2004,
																   0,
																   0,
																   0});
			this.numNam.Name = "numNam";
			this.numNam.Size = new System.Drawing.Size(48, 21);
			this.numNam.TabIndex = 9;
			this.numNam.Value = new System.Decimal(new int[] {
																 2004,
																 0,
																 0,
																 0});
			this.numNam.Enter += new System.EventHandler(this.numNam_Enter);
			this.numNam.KeyDown += new System.Windows.Forms.KeyEventHandler(this.event_KeyDown);
			this.numNam.Leave += new System.EventHandler(this.numNam_Leave);
			// 
			// butIn
			// 
			this.butIn.BackColor = System.Drawing.SystemColors.Control;
			this.butIn.ForeColor = System.Drawing.Color.DarkBlue;
			this.butIn.Image = ((System.Drawing.Bitmap)(resources.GetObject("butIn.Image")));
			this.butIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butIn.Location = new System.Drawing.Point(221, 120);
			this.butIn.Name = "butIn";
			this.butIn.Size = new System.Drawing.Size(60, 25);
			this.butIn.TabIndex = 22;
			this.butIn.Text = "     &In";
			this.butIn.Click += new System.EventHandler(this.butIn_Click);
			this.butIn.KeyDown += new System.Windows.Forms.KeyEventHandler(this.event_KeyDown);
			// 
			// label4
			// 
			this.label4.BackColor = System.Drawing.SystemColors.Control;
			this.label4.ForeColor = System.Drawing.Color.DarkBlue;
			this.label4.Location = new System.Drawing.Point(8, 72);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(72, 23);
			this.label4.TabIndex = 14;
			this.label4.Text = "Loại khoản :";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label8
			// 
			this.label8.BackColor = System.Drawing.SystemColors.Control;
			this.label8.ForeColor = System.Drawing.Color.DarkBlue;
			this.label8.Location = new System.Drawing.Point(17, 17);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(56, 23);
			this.label8.TabIndex = 2;
			this.label8.Text = "Từ tháng";
			this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// butKetthuc
			// 
			this.butKetthuc.BackColor = System.Drawing.SystemColors.Control;
			this.butKetthuc.ForeColor = System.Drawing.Color.DarkBlue;
			this.butKetthuc.Image = ((System.Drawing.Bitmap)(resources.GetObject("butKetthuc.Image")));
			this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butKetthuc.Location = new System.Drawing.Point(283, 120);
			this.butKetthuc.Name = "butKetthuc";
			this.butKetthuc.Size = new System.Drawing.Size(68, 25);
			this.butKetthuc.TabIndex = 23;
			this.butKetthuc.Text = "&Kết thúc";
			this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
			this.butKetthuc.KeyDown += new System.Windows.Forms.KeyEventHandler(this.event_KeyDown);
			// 
			// txtTenloaikhoan
			// 
			this.txtTenloaikhoan.BackColor = System.Drawing.SystemColors.HighlightText;
			this.txtTenloaikhoan.CausesValidation = false;
			this.txtTenloaikhoan.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtTenloaikhoan.ForeColor = System.Drawing.Color.DarkBlue;
			this.txtTenloaikhoan.Location = new System.Drawing.Point(263, 70);
			this.txtTenloaikhoan.Name = "txtTenloaikhoan";
			this.txtTenloaikhoan.ReadOnly = true;
			this.txtTenloaikhoan.Size = new System.Drawing.Size(235, 21);
			this.txtTenloaikhoan.TabIndex = 17;
			this.txtTenloaikhoan.TabStop = false;
			this.txtTenloaikhoan.Text = "";
			// 
			// label13
			// 
			this.label13.BackColor = System.Drawing.SystemColors.Control;
			this.label13.ForeColor = System.Drawing.Color.DarkBlue;
			this.label13.Location = new System.Drawing.Point(-7, 44);
			this.label13.Name = "label13";
			this.label13.Size = new System.Drawing.Size(88, 23);
			this.label13.TabIndex = 10;
			this.label13.Text = "Số tài khoản :";
			this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// txtTentaikhoan
			// 
			this.txtTentaikhoan.BackColor = System.Drawing.SystemColors.HighlightText;
			this.txtTentaikhoan.CausesValidation = false;
			this.txtTentaikhoan.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtTentaikhoan.ForeColor = System.Drawing.Color.DarkBlue;
			this.txtTentaikhoan.Location = new System.Drawing.Point(263, 43);
			this.txtTentaikhoan.Name = "txtTentaikhoan";
			this.txtTentaikhoan.ReadOnly = true;
			this.txtTentaikhoan.Size = new System.Drawing.Size(235, 21);
			this.txtTentaikhoan.TabIndex = 13;
			this.txtTentaikhoan.TabStop = false;
			this.txtTentaikhoan.Text = "";
			// 
			// numDenthang
			// 
			this.numDenthang.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.numDenthang.ForeColor = System.Drawing.Color.DarkBlue;
			this.numDenthang.Location = new System.Drawing.Point(143, 16);
			this.numDenthang.Maximum = new System.Decimal(new int[] {
																		12,
																		0,
																		0,
																		0});
			this.numDenthang.Minimum = new System.Decimal(new int[] {
																		1,
																		0,
																		0,
																		0});
			this.numDenthang.Name = "numDenthang";
			this.numDenthang.Size = new System.Drawing.Size(37, 21);
			this.numDenthang.TabIndex = 7;
			this.numDenthang.Value = new System.Decimal(new int[] {
																	  1,
																	  0,
																	  0,
																	  0});
			this.numDenthang.Enter += new System.EventHandler(this.numDenthang_Enter);
			this.numDenthang.KeyDown += new System.Windows.Forms.KeyEventHandler(this.event_KeyDown);
			this.numDenthang.Leave += new System.EventHandler(this.numDenthang_Leave);
			// 
			// lblTentk
			// 
			this.lblTentk.BackColor = System.Drawing.SystemColors.Control;
			this.lblTentk.ForeColor = System.Drawing.Color.DarkBlue;
			this.lblTentk.Location = new System.Drawing.Point(170, 44);
			this.lblTentk.Name = "lblTentk";
			this.lblTentk.Size = new System.Drawing.Size(96, 23);
			this.lblTentk.TabIndex = 12;
			this.lblTentk.Text = "Tên tài khoản :";
			this.lblTentk.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// lblTenlk
			// 
			this.lblTenlk.BackColor = System.Drawing.SystemColors.Control;
			this.lblTenlk.ForeColor = System.Drawing.Color.DarkBlue;
			this.lblTenlk.Location = new System.Drawing.Point(155, 72);
			this.lblTenlk.Name = "lblTenlk";
			this.lblTenlk.Size = new System.Drawing.Size(112, 23);
			this.lblTenlk.TabIndex = 16;
			this.lblTenlk.Text = "Tên loại khoản :";
			this.lblTenlk.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label2
			// 
			this.label2.BackColor = System.Drawing.SystemColors.Control;
			this.label2.ForeColor = System.Drawing.Color.DarkBlue;
			this.label2.Location = new System.Drawing.Point(109, 17);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(32, 23);
			this.label2.TabIndex = 6;
			this.label2.Text = "đến ";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label1
			// 
			this.label1.BackColor = System.Drawing.SystemColors.Control;
			this.label1.ForeColor = System.Drawing.Color.DarkBlue;
			this.label1.Location = new System.Drawing.Point(180, 17);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(34, 23);
			this.label1.TabIndex = 8;
			this.label1.Text = "năm";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// butXem
			// 
			this.butXem.BackColor = System.Drawing.SystemColors.Control;
			this.butXem.ForeColor = System.Drawing.SystemColors.WindowText;
			this.butXem.Image = ((System.Drawing.Bitmap)(resources.GetObject("butXem.Image")));
			this.butXem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butXem.Location = new System.Drawing.Point(159, 120);
			this.butXem.Name = "butXem";
			this.butXem.Size = new System.Drawing.Size(60, 25);
			this.butXem.TabIndex = 52;
			this.butXem.Text = "     &Xem";
			this.butXem.Click += new System.EventHandler(this.butXem_Click);
			// 
			// listLoaikhoan
			// 
			this.listLoaikhoan.BackColor = System.Drawing.SystemColors.Info;
			this.listLoaikhoan.ColumnCount = 0;
			this.listLoaikhoan.Location = new System.Drawing.Point(424, 368);
			this.listLoaikhoan.MatchBufferTimeOut = 1000;
			this.listLoaikhoan.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
			this.listLoaikhoan.Name = "listLoaikhoan";
			this.listLoaikhoan.Size = new System.Drawing.Size(75, 17);
			this.listLoaikhoan.TabIndex = 106;
			this.listLoaikhoan.TextIndex = -1;
			this.listLoaikhoan.TextMember = null;
			this.listLoaikhoan.ValueIndex = -1;
			this.listLoaikhoan.Visible = false;
			// 
			// listTaikhoan
			// 
			this.listTaikhoan.BackColor = System.Drawing.SystemColors.Info;
			this.listTaikhoan.ColumnCount = 0;
			this.listTaikhoan.Location = new System.Drawing.Point(424, 368);
			this.listTaikhoan.MatchBufferTimeOut = 1000;
			this.listTaikhoan.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
			this.listTaikhoan.Name = "listTaikhoan";
			this.listTaikhoan.Size = new System.Drawing.Size(75, 17);
			this.listTaikhoan.TabIndex = 106;
			this.listTaikhoan.TextIndex = -1;
			this.listTaikhoan.TextMember = null;
			this.listTaikhoan.ValueIndex = -1;
			this.listTaikhoan.Visible = false;
			// 
			// frmSocai
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(510, 171);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.butXem,
																		  this.txtTentaikhoan,
																		  this.txtSotaikhoan,
																		  this.txtLoaikhoan,
																		  this.numDenthang,
																		  this.numTuthang,
																		  this.txtTenloaikhoan,
																		  this.butKetthuc,
																		  this.butIn,
																		  this.lblTenlk,
																		  this.label4,
																		  this.lblTentk,
																		  this.label13,
																		  this.numNam,
																		  this.label1,
																		  this.label2,
																		  this.listLoaikhoan,
																		  this.listTaikhoan,
																		  this.label8});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.KeyPreview = true;
			this.Name = "frmSocai";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Sổ Cái Tổng Hợp";
			this.TopMost = true;
			this.Load += new System.EventHandler(this.frmSocai_Load);
			((System.ComponentModel.ISupportInitialize)(this.numTuthang)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.numNam)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.numDenthang)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void frmSocai_Load(object sender, System.EventArgs e)
		{
			//Gan gia tri khoi tao cho cac o thang nam
			numTuthang.Focus();
			numTuthang.Value=DateTime.Now.Month;
			numDenthang.Value=DateTime.Now.Month;
			numNam.Value=DateTime.Now.Year;
			numNam.Value=DateTime.Now.Year;
			//
			listTaikhoan.DisplayMember="TTK";
			listTaikhoan.TextMember="STK";
			listTaikhoan.ValueMember="TTK";

			listLoaikhoan.DisplayMember="TLK";
			listLoaikhoan.TextMember="LK";
			listLoaikhoan.ValueMember="TLK";
			
			load_dm();
			txtSotaikhoan.Focus();
		}

		private void load_dm()
		{
			sql="select trim(stk) STK,trim(ttk) TTK,id,sp,hethong from a_dmtk order by stk";
			dtDmtk=a.get_data(sql).Tables[0];
			listTaikhoan.DataSource=dtDmtk;
			
			sql="select trim(lk) LK,trim(tlk) TLK,id from a_dmlk order by lk";
			dtDmlk=a.get_data(sql).Tables[0];
			listLoaikhoan.DataSource=dtDmlk;
		}


		private void event_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab || e.KeyValue==13)
			{
				SendKeys.Send("{Tab}");
			}
		}
		
		private void numTuthang_Enter(object sender, System.EventArgs e)
		{
			this.numTuthang.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(i_Red)), ((System.Byte)(i_Green)), ((System.Byte)(i_Blue)));
		}

		private void numDenthang_Enter(object sender, System.EventArgs e)
		{
			this.numDenthang.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(i_Red)), ((System.Byte)(i_Green)), ((System.Byte)(i_Blue)));
		}

		private void numNam_Enter(object sender, System.EventArgs e)
		{
			this.numNam.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(i_Red)), ((System.Byte)(i_Green)), ((System.Byte)(i_Blue)));
		}

		private void txtSotaikhoan_Enter(object sender, System.EventArgs e)
		{
			this.txtSotaikhoan.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(i_Red)), ((System.Byte)(i_Green)), ((System.Byte)(i_Blue)));
		}

		private void txtLoaikhoan_Enter(object sender, System.EventArgs e)
		{
			this.txtLoaikhoan.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(i_Red)), ((System.Byte)(i_Green)), ((System.Byte)(i_Blue)));
		}

		private void numTuthang_Leave(object sender, System.EventArgs e)
		{
			this.numTuthang.BackColor = System.Drawing.SystemColors.Window;
		}

		private void numDenthang_Leave(object sender, System.EventArgs e)
		{
			this.numDenthang.BackColor = System.Drawing.SystemColors.Window;
		}

		private void numNam_Leave(object sender, System.EventArgs e)
		{
			this.numNam.BackColor = System.Drawing.SystemColors.Window;
		}

		private void numSotaikhoan_Leave(object sender, System.EventArgs e)
		{
			this.txtSotaikhoan.BackColor = System.Drawing.SystemColors.Window;
		}

		private void numLoaikhoan_Leave(object sender, System.EventArgs e)
		{
			this.txtLoaikhoan.BackColor = System.Drawing.SystemColors.Window;
		}


		private void Filter_List(string s_Dkloc,int i_Loai)
		{
			try
			{
				if (i_Loai==1 || i_Loai==2)
				{
					CurrencyManager cm= (CurrencyManager)BindingContext[listTaikhoan.DataSource];
					System.Data.DataView dv=(System.Data.DataView)cm.List;
					if (i_Loai==1) dv.RowFilter="stk like '%"+s_Dkloc.Trim()+"%'";
					else if (i_Loai==2) dv.RowFilter="ttk like '%"+s_Dkloc.Trim()+"%'";
				}
				else if (i_Loai==3 || i_Loai==4)
				{
					CurrencyManager cm= (CurrencyManager)BindingContext[listLoaikhoan.DataSource];
					System.Data.DataView dv=(System.Data.DataView)cm.List;
					if (i_Loai==3) dv.RowFilter="lk like '%"+s_Dkloc.Trim()+"%'";
					else if (i_Loai==4) dv.RowFilter="tlk like '%"+s_Dkloc.Trim()+"%'";
				}
			}
			catch{}
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void txtSotaikhoan_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Down || e.KeyCode==Keys.Up) listTaikhoan.Focus();
			else if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab)
			{
				if (listTaikhoan.Visible) listTaikhoan.Focus();
				else SendKeys.Send("{Tab}");
			}
		}

		private void txtSotaikhoan_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==txtSotaikhoan)
			{
				Filter_List(txtSotaikhoan.Text,1);
				listTaikhoan.BrowseToDmtk(txtSotaikhoan,txtTentaikhoan,txtLoaikhoan,txtSotaikhoan.Location.X,txtSotaikhoan.Location.Y + txtSotaikhoan.Height-2,txtSotaikhoan.Width+lblTentk.Width+txtTentaikhoan.Width,txtSotaikhoan.Height+5,100);
			}	
		}

		private void txtSotaikhoan_DoubleClick(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==txtSotaikhoan)
			{
				Filter_List(txtSotaikhoan.Text,1);
				listTaikhoan.BrowseToDmtk(txtSotaikhoan,txtTentaikhoan,txtLoaikhoan,txtSotaikhoan.Location.X,txtSotaikhoan.Location.Y + txtSotaikhoan.Height-2,txtSotaikhoan.Width+lblTentk.Width+txtTentaikhoan.Width,txtSotaikhoan.Height+5,100);
			}	
		}

		private void txtSotaikhoan_Validated(object sender, System.EventArgs e)
		{
			txtTentaikhoan.Text="";
			if (txtSotaikhoan.Text!="")
			{
				r=a.getrowbyid(dtDmtk,"stk='"+txtSotaikhoan.Text.Trim()+"'");
				if (r!=null) 
				{
					txtTentaikhoan.Text=r["ttk"].ToString();
				}
			}
			if(!listTaikhoan.Focused) listTaikhoan.Hide();
			if(!listLoaikhoan.Focused) listLoaikhoan.Hide();
		}

		private void txtLoaikhoan_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Down || e.KeyCode==Keys.Up) listLoaikhoan.Focus();
			else if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab)
			{
				if (listLoaikhoan.Visible) listLoaikhoan.Focus();
				else SendKeys.Send("{Tab}");
			}
		}

		private void txtLoaikhoan_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==txtLoaikhoan)
			{
				Filter_List(txtLoaikhoan.Text,3);
				listLoaikhoan.BrowseToDmtk(txtLoaikhoan,txtTenloaikhoan,butXem,txtLoaikhoan.Location.X,txtLoaikhoan.Location.Y + txtLoaikhoan.Height-2,txtLoaikhoan.Width+lblTenlk.Width+txtTenloaikhoan.Width,txtLoaikhoan.Height+5,100);
			}	
		}

		private void txtLoaikhoan_DoubleClick(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==txtLoaikhoan)
			{
				Filter_List(txtLoaikhoan.Text,3);
				listLoaikhoan.BrowseToDmtk(txtLoaikhoan,txtTenloaikhoan,butXem,txtLoaikhoan.Location.X,txtLoaikhoan.Location.Y + txtLoaikhoan.Height-2,txtLoaikhoan.Width+lblTenlk.Width+txtTenloaikhoan.Width,txtLoaikhoan.Height+5,100);
			}	
		}
		private void txtLoaikhoan_Validated(object sender, System.EventArgs e)
		{
			txtTenloaikhoan.Text="";
			if (txtLoaikhoan.Text!="")
			{
				r=a.getrowbyid(dtDmlk,"lk='"+txtLoaikhoan.Text.Trim()+"'");
				if (r!=null) 
				{
					txtTenloaikhoan.Text=r["tlk"].ToString();
				}
			}
			if(!listTaikhoan.Focused) listTaikhoan.Hide();
			if(!listLoaikhoan.Focused) listLoaikhoan.Hide();
		}

		private void Display(int loai)
		//Loai display: 1 - xuat ra man hinh; 2 - xuat ra may in. 
		{
			if (numDenthang.Value<numTuthang.Value) numDenthang.Value=numTuthang.Value;
			decimal sumAll=0;

			dsData=new System.Data.DataSet();
			int yy=int.Parse(numNam.Value.ToString()), mm1=int.Parse(numTuthang.Value.ToString());
			int mm2=int.Parse(numDenthang.Value.ToString());
			string s_yy=yy.ToString().PadLeft(4,'0').Substring(2,2);
			string s_Tungay=numTuthang.Value.ToString().PadLeft(2,'0')+"/"+numNam.Value.ToString().PadLeft(4,'0');
			string s_Denngay=numDenthang.Value.ToString().PadLeft(2,'0')+"/"+numNam.Value.ToString().PadLeft(4,'0');
			string s_IDtk="",s_IDlk="",s_mmyyyy="";

			if (txtSotaikhoan.Text=="")
			{
				MessageBox.Show("Vui lòng nhập số tài khoản!","Chú ý!");
				txtSotaikhoan.Focus();
				return;
			}

			string s_Thangnam=a.kt_Tinhsodu(mm1.ToString(),s_yy);
			if (s_Thangnam!="")
			{
				MessageBox.Show("Các tháng "+s_Thangnam.Substring(0,s_Thangnam.Length-1)+" chưa tính số dư!","Chú ý!");
				return;
			}

			try
			{
				int i_LengthTK=txtSotaikhoan.Text.ToString().Trim().Length;
				sql="select id from a_dmtk where substr(stk,1,"+i_LengthTK+")='"+txtSotaikhoan.Text.Trim()+"' order by id";
				foreach(System.Data.DataRow dr in a.get_data(sql).Tables[0].Rows)
				{ 
					s_IDtk+=dr["id"].ToString()+",";
				}
				if (s_IDtk.Substring(s_IDtk.Length-1,1)==",") s_IDtk=s_IDtk.Substring(0,s_IDtk.Length-1);
				if (txtLoaikhoan.Text!="") s_IDlk=a.getrowbyid(dtDmlk,"lk='"+txtLoaikhoan.Text.Trim()+"'")["id"].ToString().Trim();
			}
			catch{}	;

			sql=  "select distinct to_char(ct.ngayg,'mm/yyyy') thangg,ct.ngaygs ngaygs,ct.soctg,ct.ngayg ngayg,";
			sql+=" nvl(ctct.noidung,ct.noidung) diengiai,(case when ctct.tknoa in("+s_IDtk+") then dmtkco.stk else (case when";
			sql+=" ctct.tkcoa in("+s_IDtk+") then dmtkno.stk else '' end) end) tkdoiung,";
			sql+=" (case when ctct.tknoa in("+s_IDtk+") then ctct.stien else 0 end) sotienno,";
			sql+=" (case when ctct.tkcoa in("+s_IDtk+") then ctct.stien else 0 end) sotienco,";
			sql+=" 0 tondauno,0 tondauco,0 fsnoquy,0 fscoquy,0 fsnonam,0 fsconam,'' ghichu,";
			sql+=" (case when ctct.tknoa in("+s_IDtk+") then 1 else 2 end) loaictu,to_number(to_char(ct.ngayg,'yyyymm')||'2') sapxep";
			sql+=" from a_ct"+s_yy+" ct,a_ctct"+s_yy+" ctct,a_dmlk lk,a_dmtk dmtkno,a_dmtk dmtkco"; 
			sql+=" where ct.id=ctct.idct and ctct.lk=lk.id(+) and ctct.tonghop=1";
			sql+=" and ctct.tknoa=dmtkno.id and ctct.tkcoa=dmtkco.id";
			sql+=" and to_date(to_char(ct.ngayg,'mm/yyyy'),'mm/yyyy') between to_date('"+s_Tungay+"','mm/yyyy') and to_date('"+s_Denngay+"','mm/yyyy')";
			if (s_IDlk!="") sql+=" and lk.id='"+s_IDlk+"'";
			sql+=" and (ctct.tknoa in("+s_IDtk+") or ctct.tkcoa in("+s_IDtk+"))";
			sql+=" order by ct.ngayg";

			try
			{
				dsData=a.get_data(sql).Copy();
			}
			catch{};

			try
			{
				decimal d_Daukyno=0,d_Daukyco=0,d_FSquyno=0,d_FSquyco=0,d_FSnamno=0,d_FSnamco=0;
				//Them dong load so du dau ky.
				for (int j=mm1;j<=mm2;j++)
				{
					try
					{ 
						System.Data.DataRow drSodu = dsData.Tables[0].NewRow();
						drSodu["thangg"]=j.ToString().PadLeft(2,'0')+"/"+yy.ToString().PadLeft(4,'0');
						drSodu["ngaygs"]=DateTime.Parse(j.ToString().PadLeft(2,'0')+"/01/"+yy.ToString());
						drSodu["soctg"]="";
						drSodu["ngayg"]=DateTime.Parse(j.ToString().PadLeft(2,'0')+"/01/"+yy.ToString());
						drSodu["diengiai"]="Số dư đầu tháng";
						drSodu["tkdoiung"]="";

						//Tham so cuoi: 1-lay so du no hoac phat sinh no, 2-lay so du co hoac phat sinh co.
						drSodu["sotienno"]=0;
						drSodu["sotienco"]=0;
						
						drSodu["ghichu"]="";
						drSodu["loaictu"]=0;
						drSodu["sapxep"]=int.Parse(yy.ToString()+j.ToString().PadLeft(2,'0')+"1");

						//Set lai phat sinh quy khi qua quy moi.
						if (((j-1) % 3)==0)
						{
							d_FSquyno=0;
							d_FSquyco=0;
						}
						//

						if (j==mm1)
						{						
							d_Daukyno=a.get_Sodudauky(j.ToString().PadLeft(2,'0'),s_yy,s_IDtk,s_IDlk,1);
							d_Daukyco=a.get_Sodudauky(j.ToString().PadLeft(2,'0'),s_yy,s_IDtk,s_IDlk,2);

							d_FSquyno=a.get_FSluykequy(j.ToString().PadLeft(2,'0'),s_yy,s_IDtk,s_IDlk,1);
							d_FSquyco=a.get_FSluykequy(j.ToString().PadLeft(2,'0'),s_yy,s_IDtk,s_IDlk,2);

							d_FSnamno=a.get_FSluykenam(j.ToString().PadLeft(2,'0'),s_yy,s_IDtk,s_IDlk,1);
							d_FSnamco=a.get_FSluykenam(j.ToString().PadLeft(2,'0'),s_yy,s_IDtk,s_IDlk,2);
							sumAll+=d_Daukyno+d_Daukyco+d_FSquyno+d_FSquyco+d_FSnamno+d_FSnamco;
						}
						else
						{
							s_mmyyyy=(j-1).ToString().PadLeft(2,'0')+"/"+numNam.Value.ToString().PadLeft(4,'0');
							decimal d_Luyke_ds_no=a.get_FSluyke_ds("thangg='"+s_mmyyyy+"'",dsData,"sotienno","sotienco",1);
							decimal d_Luyke_ds_co=a.get_FSluyke_ds("thangg='"+s_mmyyyy+"'",dsData,"sotienno","sotienco",2);

							d_Daukyno=(d_Daukyno+d_Luyke_ds_no) - Math.Min((d_Daukyno+d_Luyke_ds_no),(d_Daukyco+d_Luyke_ds_co));
							d_Daukyco=(d_Daukyco+d_Luyke_ds_co) - Math.Min((d_Daukyno+d_Luyke_ds_no),(d_Daukyco+d_Luyke_ds_co));

							d_FSquyno+=d_Luyke_ds_no;
							d_FSquyco+=d_Luyke_ds_co;

							d_FSnamno+=d_Luyke_ds_no;
							d_FSnamco+=d_Luyke_ds_co;
							sumAll+=d_Daukyno+d_Daukyco+d_FSquyno+d_FSquyco+d_FSnamno+d_FSnamco;
						}						
						drSodu["tondauno"]=d_Daukyno;
						drSodu["tondauco"]=d_Daukyco;
						
						drSodu["fsnoquy"]=d_FSquyno;
						drSodu["fscoquy"]=d_FSquyco;

						drSodu["fsnonam"]=d_FSnamno;
						drSodu["fsconam"]=d_FSnamco;

						dsData.Tables[0].Rows.Add(drSodu);
						dsData.AcceptChanges();
					}
					catch{}	;
				}
			}
			catch{};
			try
			{
				if (dsData==null || dsData.Tables[0].Rows.Count<=0 ) MessageBox.Show("Không có số liệu !","Thông báo !");
				else
				{
					//dsData.WriteXml("..\\..\\xml_data\\kt_Socai.xml",XmlWriteMode.WriteSchema);
					s_Report="KT_Socai.rpt";
					string title=""; 
					if (mm1==mm2) title="Tháng "+mm1+" năm "+yy;
					else title="Từ tháng "+mm1+" đến tháng "+mm2+" năm "+yy;

					string title2="Tài khoản : "+txtSotaikhoan.Text+" - "+txtTentaikhoan.Text;
					string title3="";
					
					if (loai==1)
					{
						frmReport f=new frmReport(a,dsData,title,title2,title3,s_Report.ToString());
						f.ShowDialog();
					}
					else Print.Printer(a,dsData,title,title2,title3,s_Report.ToString(),1,1);
				}
			}
			catch
			{
				MessageBox.Show("Không có số liệu !","Thông báo !");
			}
		}

		private void butXem_Click(object sender, System.EventArgs e)
		{
			Display(1);
		}

		private void butIn_Click(object sender, System.EventArgs e)
		{
			Display(2);
		}		
	}
}
